import CommonConstants from '../constants/CommonConstants';
import Logger from './Logger';

class CommonUtils {
  /**
   * Check obj is empty
   *
   * @param {object} obj
   * @return {boolean} true(empty)
   */
  isEmpty(obj: object | string): boolean {
    return obj === undefined || obj === null || obj === '';
  }

  /**
   * Check array is empty
   *
   * @param {Array} array
   * @return {boolean} true(empty)
   */
  isEmptyArr(array: Array<any>): boolean {
    return this.isEmpty(array) || array.length === 0;
  }

  /**
   * 文本滑动选择器弹窗
   * @param {string[] | Resource} dataSource 数据源
   * @param {Function(value: string)} callback 选中后回调函数
   */
  textPickerDialog(dataSource: string[] | Resource, callback: Function) {
    if (this.isEmpty(dataSource)) {
      Logger.error(CommonConstants.TAG_COMMON_UTILS, 'data source is null');
      return;
    }
    TextPickerDialog.show({
      range: dataSource,
      selected: 0,
      //点击弹窗中的“确定”按钮时触发该回调。
      onAccept: (result: TextPickerResult) => {
        callback(result.value);
      },
      //点击弹窗中的“取消”按钮时触发该回调。
      onCancel: () => {
        Logger.info(CommonConstants.TAG_COMMON_UTILS, 'TextPickerDialog onCancel');
      }
    });
  }

  /**
   * 消息提示框
   * @param {ResourceStr} title 标题
   * @param {ResourceStr} message 消息
   * @param {Function} callback 点击确定按钮回调函数
   */
  alterDialog(title: ResourceStr = $r('app.string.default_alter_dialog_title'), message: ResourceStr, callback?: Function) {
    if (this.isEmpty(message)) return;

    AlertDialog.show(
      {
        title: title,
        message: message,
        autoCancel: true,
        alignment: DialogAlignment.Center,
        gridCount: 4,
        // 点击按钮关闭dialog时的回调。
        confirm: {
          value: $r('app.string.confirm_button_text'),
          action: () => {
            callback && callback();
          }
        },
        // 点击遮障层关闭dialog时的回调。
        cancel: () => {
          callback && callback();
          Logger.info(CommonConstants.TAG_COMMON_UTILS, 'alterDialog onCancel');
        }
      }
    )
  }

  /**
   * 确认消息提示框
   * @param {ResourceStr} title 标题
   * @param {ResourceStr} message 消息
   * @param {Function} confirm 点击确定按钮回调函数
   * @param {Function} cancel 点击确取消按钮回调函数
   */
  confirmDialog(title: ResourceStr = $r('app.string.default_alter_dialog_title'), message: ResourceStr, confirm: Function, cancel?: Function) {
    if (this.isEmpty(message)) return;

    AlertDialog.show(
      {
        title: title,
        message: message,
        autoCancel: false,
        alignment: DialogAlignment.Center,
        gridCount: 4,
        primaryButton: {
          value: $r('app.string.cancel_button_text'),
          //fontColor: ResourceColor,
          //backgroundColor: ResourceColor,
          action: () => {
            cancel && cancel();
          }
        },
        secondaryButton: {
          value: $r('app.string.confirm_button_text'),
          action: () => {
            confirm && confirm();
            Logger.info(CommonConstants.TAG_COMMON_UTILS, 'confirmDialog onCancel');
          }
        },
      }
    )
  }

  /**
   * 日期选择弹出框
   * @param accept 选中回调函数
   * @param selectedDate 选中的日期
   * @param startDate 设置选择器的起始日期
   * @param endDate 设置选择器的结束日期
   * @param change 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调
   * @param cancel 点击弹窗中的“取消”按钮时触发该回调
   */
  datePickerDialog(accept: Function, selectedDate?: Date | string, startDate?: Date, endDate?: Date, change?: Function, cancel?: Function) {
    let selected = new Date();

    if (!this.isEmpty(selectedDate)) {
      selected = typeof selectedDate === 'string' ? new Date(selectedDate) : selectedDate
    }

    DatePickerDialog.show({
      start: startDate || new Date("1900-1-1"), // 设置选择器的起始日期
      end: endDate || new Date(), // 设置选择器的结束日期
      selected: selected, // 设置当前选中的日期
      lunar: false,
      onAccept: (value: DatePickerResult) => { // 点击弹窗中的“确定”按钮时触发该回调
        accept && accept(value)
        Logger.info(CommonConstants.TAG_COMMON_UTILS, "DatePickerDialog:onAccept()" + JSON.stringify(value))
      },
      onCancel: () => { // 点击弹窗中的“取消”按钮时触发该回调
        cancel && cancel()
        Logger.info(CommonConstants.TAG_COMMON_UTILS, "DatePickerDialog:onCancel()")
      },
      onChange: (value: DatePickerResult) => { // 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调
        change && change(value)
        Logger.info(CommonConstants.TAG_COMMON_UTILS, "DatePickerDialog:onChange()" + JSON.stringify(value))
      }
    })
  }
}

export default new CommonUtils();